﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Windows;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Database;
using ReflectionStudio.Core.Events;
using SDTVKVV;
namespace WPFExcelReport
{
    public partial class SDTVKVVData
    {
        public DataSet Convert2VKKOPF(DataRow dwExt)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "VKKOPF_MAP";
            DataTable tbMapID = new DataTable();

            tbMapID = SetExt.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "VKKOPF";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim() == "VKKOPF"
                        select dwMap;
            foreach (DataRow dwTemp in query)
            {
                Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp, dwExt);
            }
            tbIntex.Rows.Add(dwIntex);
            setReturn.Merge(tbIntex);
            return setReturn;
        }
        public DataSet Convert2VKPOS(DataRow dwExt)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "VKPOS_MAP";
            DataTable tbMapID = new DataTable();

            tbMapID = SetExt.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "VKPOS";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim() == "VKPOS"
                        select dwMap;
            foreach (DataRow dwTemp in query)
            {
                Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp, dwExt);
            }
            tbIntex.Rows.Add(dwIntex);
            setReturn.Merge(tbIntex);
            return setReturn;
        }
        public DataSet AllocateWB(DataRow dwExt)
        {

            DataRow dwWDECK = dwExt;
            DataRow dwWBBST;
            DataSet setResult = new DataSet();
            DataSet Return = new DataSet();
            double pcl = 0;
            DataRow dwVKPOS = SetExt.Tables["VKPOS_EXT"].Rows[0];
            pcl = System.Math.Round(System.Convert.ToDouble(dwVKPOS["VKPMENGE1"].ToString()) * 10, 0);
            dwWBBST = SetExt.Tables["WBBST"].NewRow();
            dwWBBST["WBBMANDANT"] = dwWDECK["WDMANDANT"].ToString();
            dwWBBST["WBBDISPOART"] = dwWDECK["WDART"].ToString();
            dwWBBST["WBBDISPOVAR"] = dwWDECK["WDVAR"].ToString();
            dwWBBST["WBBWAKZ"] = dwWDECK["WDWAKZ"].ToString();
            dwWBBST["WBBLO"] = ITXWBLO;
            dwWBBST["WBBLB"] = ITXWBLB;
            dwWBBST["WBBDESS"] = dwWDECK["WDDESS"].ToString();
            dwWBBST["WBBCOLORIT"] = dwWDECK["WDCOLORIT"].ToString();
            dwWBBST["WBBBESCHICH"] = dwWDECK["WDBESCHICH"].ToString();
            dwWBBST["WBBFARBE"] = dwWDECK["WDFARBE"].ToString();
            dwWBBST["WBBISTKLG"] = pcl;
            if (SetExt.Tables.Contains("WBBST_ITX"))
            {
                SetExt.Tables["WBBST_ITX"].Clear();
            }
            setResult = INTEXDBC.QueryWBBST(dwWBBST, 1);
            if (setResult.Tables.Count == 0)
                return Return;
            if (setResult.Tables[0].Rows.Count == 0)
                return Return;

            setResult.Tables[0].TableName = "WBBST_ITX";
            DataRow dwWBBST_ITX = setResult.Tables["WBBST_ITX"].Rows[0];
            /*
            DataSet setTBC = null;
            foreach (DataRow rowWB in setResult.Tables["WBBST_ITX"].Rows)
            {
                setTBC = INTEXDBC.QueryWBBEW(rowWB, 2);
                if (setTBC.Tables.Count > 0)
                {
                    if (setTBC.Tables[0].Rows.Count > 0)
                    {
                        double rowtbc =System.Convert.ToDouble( setTBC.Tables[0].Rows[0]["TBCM"].ToString());
                        double rowl = System.Convert.ToDouble(rowWB["WBBISTKLG"].ToString());
                        if (rowl - rowtbc > pcl)
                        {
                            dwWBBST = rowWB;
                        }
                    }
                }
            }*/
            //确认能够找到布卷，并且数量足够。
            DataRow[] rows = setResult.Tables[0].Select("WBBSTKNR='" + dwWBBST_ITX["WBBSTKNR"].ToString() + "'");
            SetExt.Merge(rows);
            dwWBBST_ITX = SetExt.Tables["WBBST_ITX"].Rows[0];
            EventDispatcher.Instance.RaiseStatus("找到布卷:" + dwWBBST_ITX["WBBSTKNR"].ToString(), StatusEventType.RunningProgress);
            if (SetExt.Tables.Contains("WBDAT_ITX"))
            {
                SetExt.Tables["WBDAT_ITX"].Clear();
            }
            DataRow dwWBDAT = SetExt.Tables["WBDAT"].NewRow();
            dwWBDAT["WBMANDANT"] = dwWBBST_ITX["WBBMANDANT"].ToString();
            dwWBDAT["WBRWKRS"] = dwWBBST_ITX["WBBRWKRS"].ToString();
            dwWBDAT["WBRWLFD"] = dwWBBST_ITX["WBBRWLFD"].ToString();
            setResult = INTEXDBC.QueryWBDAT(dwWBDAT);
            if (setResult.Tables.Count == 0)
                return Return;
            if (setResult.Tables[0].Rows.Count == 0)
                return Return;
            setResult.Tables[0].TableName = "WBDAT_ITX";
            SetExt.Merge(setResult);
            ///Generate the WBBST
            setResult = StartWBBST(dwWBBST_ITX);
            DataRow newWB = setResult.Tables[0].Rows[0];
            Return.Merge(setResult);

            dwWBBST_ITX["WBBSPSTA"] = "T";
            double WBBTSTKLG = System.Convert.ToDouble(dwWBBST_ITX["WBBTSTKLG"].ToString());
            WBBTSTKLG += System.Convert.ToDouble(newWB["WBBSSTKLG"].ToString());
            dwWBBST_ITX["WBBTSTKLG"] = System.Math.Round(WBBTSTKLG, 0);
            double WBBTANZ = System.Convert.ToDouble(dwWBBST_ITX["WBBTANZ"].ToString());
            WBBTANZ++;
            dwWBBST_ITX["WBBTANZ"] = Right("00" + WBBTANZ, 2);
            double WBBZMT = System.Convert.ToDouble(dwWBBST_ITX["WBBZMT"].ToString());
            dwWBBST_ITX["WBBZMT"] = System.Math.Round(WBBZMT + 1, 0);
            //dwWBBST["WBBFILLER"] = "dwWBBST";
            INTEXDBC.UpdateWBBST(dwWBBST_ITX);

            dwWBDAT = SetExt.Tables["WBDAT_ITX"].Rows[0];
            INTEXDBC.UpdateWBDAT(dwWBDAT);
            DataRow dwWGBST;
            dwWGBST = SetExt.Tables["WGBST"].NewRow();
            dwWGBST["WGMANDANT"] = newWB["WBBMANDANT"].ToString();
            dwWGBST["WGWAKZ"] = newWB["WBBWAKZ"].ToString();
            dwWGBST["WGART"] = newWB["WBBART"].ToString();
            dwWGBST["WGVAR"] = newWB["WBBVAR"].ToString();
            dwWGBST["WGFARBE"] = newWB["WBBFARBE"].ToString();
            dwWGBST["WGBESCHICH"] = newWB["WBBBESCHICH"].ToString();
            dwWGBST["WGDESS"] = newWB["WBBDESS"].ToString();
            dwWGBST["WGCOLORIT"] = newWB["WBBCOLORIT"].ToString();
            dwWGBST["WGLO"] = newWB["WBBLO"].ToString();

            if (SetExt.Tables.Contains("WGBST_ITX"))
            {
                SetExt.Tables["WGBST_ITX"].Clear();
            }
            setResult = INTEXDBC.QueryWGBST(dwWGBST, 1);
            if (setResult.Tables.Count == 0)
                return Return;
            if (setResult.Tables[0].Rows.Count == 0)
                return Return;
            setResult.Tables[0].TableName = "WGBST_ITX";

            dwWGBST = setResult.Tables[0].Rows[0];
            double WGMEZUTEI = System.Convert.ToDouble(dwWGBST["WGMEZUTEI"].ToString());
            double WGMEZUTEIKG = System.Convert.ToDouble(dwWGBST["WGMEZUTEIKG"].ToString());

            double WBBSGEW = System.Convert.ToDouble(newWB["WBBSGEW"].ToString());
            double WBBSSTKLG = System.Convert.ToDouble(newWB["WBBSSTKLG"].ToString());
            WGMEZUTEI += WBBSSTKLG;
            WGMEZUTEIKG += WBBSGEW;
            dwWGBST["WGAENB"] = newWB["WBBNEUBEN"].ToString();
            dwWGBST["WGAEND"] = newWB["WBBNEUDAT"].ToString();
            dwWGBST["WGMEZUTEI"] = System.Math.Round(WGMEZUTEI, 0);
            dwWGBST["WGMEZUTEIKG"] = System.Math.Round(WGMEZUTEIKG, 0);
            INTEXDBC.UpdateWGBST(dwWGBST);
            SetExt.Merge(setResult);

            setResult = StartWBBEW(newWB);
            Return.Merge(setResult);

            DataRow dwWBBEW = setResult.Tables[0].Rows[0];
            EventDispatcher.Instance.RaiseStatus("插入剪布需求:" + dwWBBEW["WBEWSTKNR"].ToString(), StatusEventType.RunningProgress);
            INTEXDBC.InsertWBBEW(dwWBBEW);
            string expression = "";
            string krs = "", lfd = "", pos = "";
            #region====Update VKPOS===========
            var query = from row in SetExt.Tables["VKPOS"].AsEnumerable()
                        where row["VKPAUFKRS"].ToString() == dwWDECK["WDAUFKRS"].ToString()
                        && row["VKPAUFLFD"].ToString() == dwWDECK["WDAUFLFD"].ToString()
                        && row["VKPAUFPOS"].ToString() == dwWDECK["WDAUFPOS"].ToString()
                        select row;
            foreach (DataRow row in query)
            {
                row["VKPZUGET"] = row["VKPMENGE1"];
                row["VKPZUGETT1"] = row["VKPMENGE1T1"];
                row["VKPZUGETT2"] = row["VKPMENGE1T2"];
                row["VKPZUGETT3"] = row["VKPMENGE1T3"];
                row["VKPZUGETT4"] = row["VKPMENGE1T4"];
            }
            #endregion
            return Return;
        }
        public DataSet ShipWBBEW(DataRow dwVKKOPF, DataRow dwWBBST, DataRow dwWDECK)
        {
            DataSet Return = new DataSet();
            DataSet Result = new DataSet();
            if (SetExt.Tables["WBBEW"].Rows.Count == 0)
                return Return;
            bool isVVLIEF = false;
            if (SetExt.Tables.Contains("VVLIEF"))
            {
                if (SetExt.Tables["VVLIEF"].Rows.Count > 0)
                {
                    isVVLIEF = true;
                }
            }
            if (!isVVLIEF)
            {
                setResult = StartVVLIEF(dwVKKOPF);
                Return.Merge(setResult);
            }
            setResult = StartVVTLIEF(dwWBBST);
            DataRow dwVVTLIEF = setResult.Tables[0].Rows[0];
            EventDispatcher.Instance.RaiseStatus("产生发货单:" + dwVVTLIEF["VVTLIEFSCH"].ToString(), StatusEventType.RunningProgress);
            Return.Merge(setResult);
            string krs, lfd, pos;
            krs = dwWDECK["WDAUFKRS"].ToString();
            lfd = dwWDECK["WDAUFLFD"].ToString();
            pos = dwWDECK["WDAUFPOS"].ToString();
            #region====Update VKPOS===========
            var query = from row in SetExt.Tables["VKPOS"].AsEnumerable()
                        where row["VKPAUFKRS"].ToString() == dwWDECK["WDAUFKRS"].ToString()
                        && row["VKPAUFLFD"].ToString() == dwWDECK["WDAUFLFD"].ToString()
                        && row["VKPAUFPOS"].ToString() == dwWDECK["WDAUFPOS"].ToString()
                        select row;
            foreach (DataRow row in query)
            {
                row["VKPFREIGE"] = row["VKPZUGET"];
                row["VKPFREIGET1"] = row["VKPZUGETT1"];
                row["VKPFREIGET2"] = row["VKPZUGETT2"];
                row["VKPFREIGET3"] = row["VKPZUGETT3"];
                row["VKPFREIGET4"] = row["VKPZUGETT4"];
                row["VKPFRDAT"] = row["VKPNEUDAT"];
                row["VKPFRBENID"] = row["VKPNEUBEN"];
                row["VKPLTEILNR"] = "0001";
            }
            query = from row in SetExt.Tables["WBBST"].AsEnumerable()
                    where row["WBBAUFKRS"].ToString() == dwWDECK["WDAUFKRS"].ToString()
                    && row["WBBAUFLFD"].ToString() == dwWDECK["WDAUFLFD"].ToString()
                    && row["WBBAUFPOS"].ToString() == dwWDECK["WDAUFPOS"].ToString()
                    select row;

            foreach (DataRow row in query)
            {
                dwWBBST = row;
                //   WBBALIEFSCH
                dwWBBST["WBBALIEFSCH"] = dwVVTLIEF["VVTLIEFSCH"];
                dwWBBST["WBBURLIEFSCH"] = dwVVTLIEF["VVTLIEFSCH"];
                dwWBBST["WBBPACKNR"] = "0001";
                dwWBBST["WBBPACKAUTO"] = "J";
            }

            #endregion
            return Return;
        }
        public DataSet CutWBBST(DataRow dwVKKOPF, DataRow dwWDECK)
        {
            DataSet Return = new DataSet();
            DataSet Result = new DataSet();

            if (SetExt.Tables["WBBST_ITX"].Rows.Count == 0)
                return Return;
            DataRow dwWBBST_ITX = SetExt.Tables["WBBST_ITX"].Rows[0];
            string krs, lfd, pos;
            krs = dwWDECK["WDAUFKRS"].ToString();
            lfd = dwWDECK["WDAUFLFD"].ToString();
            pos = dwWDECK["WDAUFPOS"].ToString();
            bool isVVLIEF = false;
            #region====Update LocateVVTLIEF===========
            var queryV = from DataRow row in SetExt.Tables["VVTLIEF"].AsEnumerable()
                         where row["VVTAUFKRS"].ToString() == krs
                         && row["VVTAUFLFD"].ToString() == lfd
                         && row["VVTAUFPOS"].ToString() == pos
                         select row;

            DataRow dwVVTLIEF = null;
            foreach (DataRow row in queryV)
            {
                dwVVTLIEF = row;
            }
            if (dwVVTLIEF == null)
                return Return;
            #endregion
            #region====Update WBBST===========
            //待发货布卷
            DataRow dwWBBST = null;
            var queryWB = from row in SetExt.Tables["WBBST"].AsEnumerable()
                          where row["WBBAUFKRS"].ToString() == dwWDECK["WDAUFKRS"].ToString()
                          && row["WBBAUFLFD"].ToString() == dwWDECK["WDAUFLFD"].ToString()
                          && row["WBBAUFPOS"].ToString() == dwWDECK["WDAUFPOS"].ToString()
                          select row;

            foreach (DataRow row in queryWB)
            {
                dwWBBST = row;
                dwWBBST["WBBISTKLG"] = dwWBBST["WBBSSTKLG"];
                dwWBBST["WBBIGEW"] = dwWBBST["WBBIGEW"];
                dwWBBST["WBBIBRT"] = dwWBBST["WBBSBRT"];

                double WBBSSTKLG = System.Convert.ToDouble(dwWBBST_ITX["WBBSSTKLG"].ToString());
                double WBBSGEW = System.Convert.ToDouble(dwWBBST_ITX["WBBSGEW"].ToString());

                double WBBISTKLG = System.Convert.ToDouble(dwWBBST_ITX["WBBISTKLG"].ToString());
                double WBBIGEW = System.Convert.ToDouble(dwWBBST_ITX["WBBIGEW"].ToString());
                
                double clen = System.Convert.ToDouble(dwWBBST["WBBSSTKLG"].ToString());//新生布卷长度
                double tweight = System.Convert.ToDouble(dwWBBST_ITX["WBBSGEW"].ToString());//总重量
                double tleng = System.Convert.ToDouble(dwWBBST_ITX["WBBSSTKLG"].ToString());//总长度

                double cweigth = clen * (tweight / tleng);
                double tclen = System.Convert.ToDouble(dwWBBST_ITX["WBBTSTKLG"].ToString());//待剪总长度
                double tcut = System.Convert.ToDouble(dwWBBST_ITX["WBBTANZ"].ToString());//待剪布卷数

                dwWBBST_ITX["WBBSSTKLG"] = System.Math.Round(tleng - clen, 0).ToString();//总长度-剪长度
                dwWBBST_ITX["WBBISTKLG"] = System.Math.Round(WBBISTKLG - clen, 0).ToString();//总长度-剪长度

                dwWBBST_ITX["WBBSGEW"] = System.Math.Round(tweight - cweigth, 0).ToString();//总重量-剪重量
                dwWBBST_ITX["WBBIGEW"] = System.Math.Round(WBBIGEW - clen * (WBBIGEW / WBBISTKLG), 0).ToString();//总重量-剪重量

                //dwWBBST_ITX["WBBISTKLG"] = dwWBBST_ITX["WBBSSTKLG"];
                //dwWBBST_ITX["WBBIGEW"] = dwWBBST_ITX["WBBSGEW"];

                dwWBBST_ITX["WBBTSTKLG"] = System.Math.Round(tclen - clen, 0).ToString();//待剪总长度
                if (tcut > 0)
                {
                    tcut--;
                    dwWBBST_ITX["WBBTANZ"] = Right("00" + System.Math.Round(tcut, 0).ToString(), 2);
                }
                if (tcut == 0)
                {
                    dwWBBST_ITX["WBBSPSTA"] = " ";
                }
                INTEXDBC.UpdateWBBST(dwWBBST_ITX);
            }


            /*002281007   
             * --WBBSSTKLG 77900 变成76900 
             * --WBBSGEW 13866 变成13688 
             --* WBBISTKLG  77900 变成 76900
             * --WBBIGEW 13866 13688
             *-- WBBSPSTA T  T
             *-- WBBTSTKLG 2000 1000
             * --WBBTANZ  02 01
            002281007-2 
             * WBBISTKLG 0  1000
             * WBBIGEW 0 178
             * WBBIBRT 0 135000
            WBBSSTKLG	WBBSGEW	WBBSBRT
             * 1000	178	135000
             * 1000	178	135000            
             */
            #endregion
            #region====Update VVLIEF===========
            #endregion
            #region====InsertWBBEW===========
            /*
             * WBBEW 在一个新的ID上增加4条记录 
             *    	SDSA	SDKEY	SDDATEN 1	0022	00BEWEGUNG            00	BEWEGUNG            000000000010000999999900000049976E  000000020001031    100000002                                                           00000000JAKV    201204230002*
             * 
             */
            setResult = StartWBBEW(dwWBBST);
            DataRow dwWBBEW = setResult.Tables[0].Rows[0];
            //WBEWBS=27
            dwWBBEW["WBEWLFDNR"] = 1;
            dwWBBEW["WBEWPOS"] = dwWBBST["WBBPOS"];
            dwWBBEW["WBEWSTKNR"] = dwWBBST["WBBSTKNR"];
            dwWBBEW["WBEWTSTKNR"] = dwWBBST_ITX["WBBSTKNR"];
            dwWBBEW["WBEWBS"] = 27;
            dwWBBEW["WBEWMTR"] = dwWBBST["WBBISTKLG"];
            dwWBBEW["WBEWGEW"] = dwWBBST["WBBIGEW"];
            DataRow dwWBBEWN = setResult.Tables[0].Rows.Add();
            //WBEWBS=64   
            dwWBBEWN.ItemArray = dwWBBEW.ItemArray;
            dwWBBEWN["WBEWLFDNR"] = 2;
            dwWBBEWN["WBEWPOS"] = dwWBBST_ITX["WBBPOS"];
            dwWBBEWN["WBEWSTKNR"] = dwWBBST_ITX["WBBSTKNR"];
            dwWBBEWN["WBEWTSTKNR"] = dwWBBST_ITX["WBBSTKNR"];
            dwWBBEWN["WBEWBS"] = 64;
            dwWBBEWN["WBEWMTR"] = "-" + dwWBBST["WBBISTKLG"].ToString();
            dwWBBEWN["WBEWGEW"] = "-" + dwWBBST["WBBIGEW"].ToString();
            dwWBBEWN = setResult.Tables[0].Rows.Add();
            //WBEWBS=61
            dwWBBEWN.ItemArray = dwWBBEW.ItemArray;
            dwWBBEWN["WBEWLFDNR"] = 3;
            dwWBBEWN["WBEWPOS"] = dwWBBST["WBBPOS"];
            dwWBBEWN["WBEWSTKNR"] = dwWBBST["WBBSTKNR"];
            dwWBBEWN["WBEWTSTKNR"] = dwWBBST_ITX["WBBSTKNR"];
            dwWBBEWN["WBEWBS"] = 61;
            dwWBBEWN["WBEWMTR"] = dwWBBST["WBBISTKLG"];
            dwWBBEWN["WBEWGEW"] = dwWBBST["WBBIGEW"];
            dwWBBEWN = setResult.Tables[0].Rows.Add();
            //WBEWBS=26  
            dwWBBEWN.ItemArray = dwWBBEW.ItemArray;
            dwWBBEWN["WBEWLFDNR"] = 4;
            dwWBBEWN["WBEWPOS"] = dwWBBST_ITX["WBBPOS"];
            dwWBBEWN["WBEWSTKNR"] = dwWBBST_ITX["WBBSTKNR"];
            dwWBBEWN["WBEWTSTKNR"] = dwWBBST_ITX["WBBSTKNR"];
            dwWBBEWN["WBEWAUFTYP"] = " ";
            dwWBBEWN["WBEWAUFKRS"] = "  ";
            dwWBBEWN["WBEWAUFLFD"] = "000000";
            dwWBBEWN["WBEWAUFPOS"] = "000";
            dwWBBEWN["WBEWTP"] = "0000";
            dwWBBEWN["WBEWBS"] = 26;
            dwWBBEWN["WBEWMTR"] = dwWBBST["WBBISTKLG"].ToString();
            dwWBBEWN["WBEWGEW"] = dwWBBST["WBBIGEW"].ToString();
            
            EventDispatcher.Instance.RaiseStatus("插入剪布需求:" + dwWBBEW["WBEWSTKNR"].ToString(), StatusEventType.RunningProgress);
            foreach (DataRow row in setResult.Tables[0].Rows)
            {
                INTEXDBC.InsertWBBEW(row);
            }
            Return.Merge(setResult);
            #endregion
            return Return;
        }
        public DataSet PackWBBST(DataRow dwVKKOPF, DataRow dwWDECK)
        {
            DataSet Return = new DataSet();
            DataSet Result = new DataSet();

            if (SetExt.Tables["WBBST_ITX"].Rows.Count == 0)
                return Return;
            DataRow dwWBBST_ITX = SetExt.Tables["WBBST_ITX"].Rows[0];
            string krs, lfd, pos;
            krs = dwWDECK["WDAUFKRS"].ToString();
            lfd = dwWDECK["WDAUFLFD"].ToString();
            pos = dwWDECK["WDAUFPOS"].ToString();
            bool isVVLIEF = false;
            #region====Update LocateVVTLIEF===========
            var queryV = from DataRow row in SetExt.Tables["VVTLIEF"].AsEnumerable()
                         where row["VVTAUFKRS"].ToString() == krs
                         && row["VVTAUFLFD"].ToString() == lfd
                         && row["VVTAUFPOS"].ToString() == pos
                         select row;

            DataRow dwVVTLIEF = null;
            foreach (DataRow row in queryV)
            {
                dwVVTLIEF = row;
            }
            if (dwVVTLIEF == null)
                return Return;
            //dwVVTLIEF["VVTRENR"] = dwVVTLIEF["VVTLIEFSCH"];
            dwVVTLIEF["VVTRESTKZ"] = "J";
            dwVVTLIEF["VVTFORT"] = "1";        
            
            #endregion
          
            #region====Update WBBST===========
            //待发货布卷
            DataRow dwWBBST = null;
            double WBBISTKLG = 0;
            var queryWB = from row in SetExt.Tables["WBBST"].AsEnumerable()
                          where row["WBBAUFKRS"].ToString() == dwWDECK["WDAUFKRS"].ToString()
                          && row["WBBAUFLFD"].ToString() == dwWDECK["WDAUFLFD"].ToString()
                          && row["WBBAUFPOS"].ToString() == dwWDECK["WDAUFPOS"].ToString()
                          select row;

            foreach (DataRow row in queryWB)
            {
                /*  002281007-13
                 *WBBPACKTYP='DJ  '
                 *WBBRUEKZ='R'	
                 * WBBURAKZ='N' 
                 * WBBLB='KO'
                 * WBBLP='        '
                 *  WBBURLB='2F'
                 *  WBBURLB='2F'
                 *  WBBURLP='19      '         
          */
                dwWBBST = row;
                dwWBBST["WBBPACKTYP"] = "DJ  ";
                dwWBBST["WBBRUEKZ"] = "R";
                dwWBBST["WBBURAKZ"] = "N";
                dwWBBST["WBBIGEW"] = dwWBBST["WBBSGEW"];
                dwWBBST["WBBURLB"] = dwWBBST["WBBLB"];
                dwWBBST["WBBURLP"] = dwWBBST["WBBLP"];
                dwWBBST["WBBLB"] = "KO";
                dwWBBST["WBBVERSANDKZ"] = "J";                
                dwWBBST["WBBLP"] = "        ";
                WBBISTKLG =System.Convert.ToDouble( dwWBBST["WBBISTKLG"].ToString());
            } 
            #endregion
            #region====Update WGBST===========
             
            DataRow dwWGBST_ITX = SetExt.Tables["WGBST_ITX"].Rows[0];
            string ITXYEAR = dwIntexStd["ITXYEAR"].ToString();
            string ITXMONTH = dwIntexStd["ITXMONTH"].ToString();
            bool wgexist=false;
            if (dwWGBST_ITX["WGMJJ"].ToString() == ITXYEAR 
                && dwWGBST_ITX["WGMM"].ToString() == ITXMONTH
                )
            {
                wgexist = true;
            }
            DataRow dwWGBST = null;
            if (!wgexist)
            {
                Result = StartWGBST(dwWGBST_ITX);
                dwWGBST = Result.Tables[0].Rows[0];
                double WBBSGEW = System.Convert.ToDouble(dwWBBST["WBBIGEW"].ToString());
                double WBBSSTKLG = System.Convert.ToDouble(dwWBBST["WBBISTKLG"].ToString());
                double WGME20 = System.Convert.ToDouble(dwWGBST["WGME20"].ToString());
                double WGME20KG = System.Convert.ToDouble(dwWGBST["WGME20KG"].ToString());
                dwWGBST["WGME20"] = System.Math.Round(WGME20 + WBBSSTKLG * 0.1, 0);
                dwWGBST["WGME20KG"] = System.Math.Round(WGME20KG + WBBSGEW * 0.1, 0);
                dwWGBST["WGME21"] = dwWGBST["WGME20"];
                dwWGBST["WGME21KG"] = dwWGBST["WGME20KG"];

                Return.Merge(Result);
            }
            else
            {
                INTEXDBC.UpdateWGBST(dwWGBST_ITX);
            }
            #endregion
            #region====InsertVVPACK===========
            setResult = StartVVPACK(dwWBBST);
            Return.Merge(setResult);
            #endregion
            #region====Update VVLIEF===========
            var queryVV = from row in SetExt.Tables["VVLIEF"].AsEnumerable()
                          where row["VVMANDANT"].ToString() == dwVVTLIEF["VVTMANDANT"].ToString()
                          && row["VVLIEFSCH"].ToString() == dwVVTLIEF["VVTLIEFSCH"].ToString()
                          select row;

            foreach (DataRow row in queryVV)
            {
                row["VVSTATUS"] = "VS";
                row["VVKZEINZEL"] = "J";
                row["VVEINZTARA"] = "0";
                row["VVEINZBRUT"] = "44";
                row["VVEINZNET"] = "44";
                row["VVANZ"] = "1";
                row["VVTARA"] = "0";
                row["VVBRUTTO"] = "44";
                row["VVNETTO"] = "44"; 
            }
            dwVVTLIEF["VVTRUECKANZSTK"] = dwVVTLIEF["VVTANZSTK"];
            dwVVTLIEF["VVTRUECKMTR"] = dwVVTLIEF["VVTMTR"];
            dwVVTLIEF["VVTRUECKKG"] = dwVVTLIEF["VVTKG"];
            dwVVTLIEF["VVTRUECKQM"] = dwVVTLIEF["VVTQM"];
            /*
             * VVSTATUS=DR
             *VVKZEINZEL	VVEINZTARA	VVEINZBRUT	VVEINZNET	VVANZ	VVTARA	VVBRUTTO	VVNETTO	VVVOLUMEN	VVANZSTK	VVNETMTR
J	11	14	3	1	11	14	3	0	1	300
VVANZTLIEFR=1
             *VVTLIEF
             VVTANZSTK	VVTMTR	VVTKG	VVTQM	VVTRUECKANZSTK	VVTRUECKMTR	VVTRUECKKG	VVTRUECKQM
1	300	0000000035	0000004050	1	300	0000000035	0000004050
1	300	0000000035	0000004050	1	300	0000000035	0000004050
1	300	0000000035	0000004050	0	0	0000000000	0000000000

             */

            #endregion

            //VVTRENR
            #region====Update VKPOS===========
            /*
             * VKPKOMMISS=VKPFREIGE
             * VKPKOMMIST1=VKPFREIGET1
             * VKPKOMMIST2=VKPFREIGET2
             * VKPKOMMIST3=VKPFREIGET3

             */
                        var query = from row in SetExt.Tables["VKPOS"].AsEnumerable()
                        where row["VKPAUFKRS"].ToString() == dwWDECK["WDAUFKRS"].ToString()
                        && row["VKPAUFLFD"].ToString() == dwWDECK["WDAUFLFD"].ToString()
                        && row["VKPAUFPOS"].ToString() == dwWDECK["WDAUFPOS"].ToString()
                        select row;
            foreach (DataRow row in query)
            {
                row["VKPKOMMISS"] = row["VKPFREIGE"];
                row["VKPKOMMIST1"] = row["VKPFREIGET1"];
                row["VKPKOMMIST2"] = row["VKPFREIGET2"];
                row["VKPKOMMIST3"] = row["VKPFREIGET3"]; 
            }          
            #endregion
        
            #region====InsertWBBEW===========
            /*
             * WBBEW 在一个新的ID上增加2条记录 
         WBEWTSTKNR="            "
             * WBEWBS=20
             * 2nd 
             * WBEWBS=21
             * WBEWPACKTYP="DJ  "	
             * WBEWLO="02"	
             * WBEWLB="KO"	
             * WBEWLP="        "

             */
            setResult = StartWBBEW(dwWBBST);
            DataRow dwWBBEW = setResult.Tables[0].Rows[0];
            //WBEWBS=20
            dwWBBEW["WBEWLFDNR"] = 1;
            dwWBBEW["WBEWTSTKNR"] = "            ";
            dwWBBEW["WBEWBS"] = 20;
            DataRow dwWBBEWN = setResult.Tables[0].Rows.Add();
            //WBEWBS=21   
            dwWBBEWN.ItemArray = dwWBBEW.ItemArray;
            dwWBBEWN["WBEWLFDNR"] = 2;
            dwWBBEWN["WBEWPACKTYP"] = "DJ  ";            
            dwWBBEWN["WBEWLB"] = "KO";
            dwWBBEWN["WBEWLP"] = "        ";
            dwWBBEWN["WBEWBS"] = 21;
            EventDispatcher.Instance.RaiseStatus("插入剪布需求:" + dwWBBEW["WBEWSTKNR"].ToString(), StatusEventType.RunningProgress);
            foreach (DataRow row in setResult.Tables[0].Rows)
            {
                INTEXDBC.InsertWBBEW(row);
            }
            Return.Merge(setResult);
            #endregion
            return Return;
        }
        
        public DataSet StartWBBST(DataRow dwExt)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "WBBST_MAP";
            DataTable tbMapID = new DataTable();
            tbMapID = SetExt.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "WBBST";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim() == "WBBST"
                        select dwMap;
            foreach (DataRow dwTemp in query)
            {
                Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp, dwExt);
            }
            tbIntex.Rows.Add(dwIntex);
            setReturn.Merge(tbIntex);
            return setReturn;
        }
        public DataSet StartWGBST(DataRow dwExt)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "WGBST_MAP";
            DataTable tbMapID = new DataTable();
            tbMapID = SetExt.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "WGBST";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim() == "WGBST"
                        select dwMap;
            foreach (DataRow dwTemp in query)
            {
                Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp, dwExt);
            }
            tbIntex.Rows.Add(dwIntex);
            setReturn.Merge(tbIntex);
            return setReturn;
        }
        public DataSet StartVVPACK(DataRow dwExt)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "VVPACK_MAP";
            DataTable tbMapID = new DataTable();
            tbMapID = SetExt.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "VVPACK";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim() == "VVPACK"
                        select dwMap;
            foreach (DataRow dwTemp in query)
            {
                Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp, dwExt);
            }
            tbIntex.Rows.Add(dwIntex);
            setReturn.Merge(tbIntex);
            return setReturn;
        }
        public DataSet StartWBBEW(DataRow dwExt)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "WBBEW_MAP";
            DataTable tbMapID = new DataTable();
            tbMapID = SetExt.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "WBBEW";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim() == "WBBEW"
                        select dwMap;
            foreach (DataRow dwTemp in query)
            {
                Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp, dwExt);
            }
            tbIntex.Rows.Add(dwIntex);
            setReturn.Merge(tbIntex);
            return setReturn;
        }
        public DataSet StartVVLIEF(DataRow dwExt)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "VVLIEF_MAP";
            DataTable tbMapID = new DataTable();
            tbMapID = SetExt.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "VVLIEF";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim() == "VVLIEF"
                        select dwMap;
            foreach (DataRow dwTemp in query)
            {
                Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp, dwExt);
            }
            tbIntex.Rows.Add(dwIntex);
            setReturn.Merge(tbIntex);
            //产生一对WBBEW完成，留位操做
            return setReturn;
        }
        public DataSet StartVVTLIEF(DataRow dwExt)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "VVTLIEF_MAP";
            DataTable tbMapID = new DataTable();
            tbMapID = SetExt.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "VVTLIEF";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim() == "VVTLIEF"
                        select dwMap;
            foreach (DataRow dwTemp in query)
            {
                Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp, dwExt);
            }
            tbIntex.Rows.Add(dwIntex);
            setReturn.Merge(tbIntex);
            //产生一对WBBEW完成，留位操做
            return setReturn;
        }
        public DataSet StartWDECK(DataRow dwExt)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "WDECK_MAP";
            DataTable tbMapID = new DataTable();
            tbMapID = SetExt.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "WDECK";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim() == "WDECK"
                        select dwMap;
            foreach (DataRow dwTemp in query)
            {
                Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp, dwExt);
            }
            tbIntex.Rows.Add(dwIntex);
            setReturn.Merge(tbIntex);
            //产生一对WBBEW完成，留位操做
            return setReturn;
        }
        private DataSet GenerateKD(ref bool IsNewCustomer, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataSet Return = new DataSet();
            if (!dwIntexStd.Table.Columns.Contains("ITXKUNDNR"))
                return Return;
            string MANDANT = dwIntexStd["ITXMANDANT"].ToString();
            string ITXVB = dwIntexStd["ITXVB"].ToString();
            string ITXVG = dwIntexStd["ITXVG"].ToString();
            string KUNDNR = dwIntexStd["ITXKUNDNR"].ToString();
            string name = dwIntexStd["ITXKUNDNAME"].ToString();
            DataRow dwKDSTAMM;
            DataRow dwKUNDVER;
            if (!KUNDNR.StartsWith("$"))
            {
                dwKDSTAMM = SetExt.Tables["KDSTAMM"].NewRow();
                dwKDSTAMM["KDMANDANT"] = MANDANT;
                dwKDSTAMM["KDKUNDNR"] = KUNDNR;
                setResult = INTEXDBC.QueryKDSTAMM(dwKDSTAMM);
                setResult.Tables[0].TableName = "KDSTAMM_ITX";
                Return.Merge(setResult);
                //

                dwKUNDVER = SetExt.Tables["KUNDVER"].NewRow();
                dwKUNDVER["KDVMANDANT"] = MANDANT;
                dwKUNDVER["KDVKUNDNR"] = KUNDNR;
                dwKUNDVER["KDVVB"] = ITXVB;
                dwKUNDVER["KDVVG"] = ITXVG;
                setResult = INTEXDBC.QueryKUNDVER(dwKUNDVER);
                setResult.Tables[0].TableName = "KUNDVER_ITX";

                Return.Merge(setResult);
            }
            return Return;
            IsNewCustomer = true;
            KUNDNR = KUNDNR.Replace("$", "");
            dwIntexStd["ITXKUNDNR"] = KUNDNR;
            foreach (DataRow row in SetExt.Tables["INTEXMAP"].Rows)
            {
                row["ITXKUNDNR"] = KUNDNR;
            }
            //get a number at first
            setResult = Convert2KDSTAMM(name);
            Return.Merge(setResult);
            setResult.Tables[0].TableName = "KDSTAMM_ITX";
            Return.Merge(setResult);
            dwKDSTAMM = setResult.Tables[0].Rows[0];
            INTEXDBC.SaveKDSTAMM(dwKDSTAMM, ITXDBVERSION);
            setResult = Convert2KUNDVER();
            Return.Merge(setResult);
            setResult.Tables[0].TableName = "KUNDVER_ITX";
            Return.Merge(setResult);
            dwKUNDVER = setResult.Tables[0].Rows[0];
            INTEXDBC.SaveKUNDVER(dwKUNDVER, ITXDBVERSION);
            return Return;
        }
        public DataSet Convert2KDSTAMM(string name)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "KDSTAMM_MAP";
            DataTable tbMapID = new DataTable();

            tbMapID = SetExt.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "KDSTAMM";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim() == "KDSTAMM"
                        select dwMap;
            foreach (DataRow dwTemp in query)
            {
                Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp);
            }
            tbIntex.Rows.Add(dwIntex);
            setReturn.Merge(tbIntex);
            return setReturn;
        }
        public DataSet Convert2KUNDVER()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string mapID = "KUNDVER_MAP";
            DataTable tbMapID = new DataTable();

            tbMapID = SetExt.Tables[mapID];
            DataTable tbIntex = new DataTable();
            tbIntex.TableName = "KUNDVER";
            DataRow dwIntex = tbIntex.NewRow();
            var query = from dwMap in tbMapID.AsEnumerable()
                        where dwMap["FILE"].ToString().Trim() == "KUNDVER"
                        select dwMap;
            foreach (DataRow dwTemp in query)
            {
                Convert2Intex.GetCovnertValue(ref dwIntex, dwTemp);
            }
            tbIntex.Rows.Add(dwIntex);
            setReturn.Merge(tbIntex);
            return setReturn;
        }
    }
}
